<template>
  <div>
    导出复盘 --- 共<InputNumber
      size="small"
      class="w60"
      v-model="selectNums"
    ></InputNumber
    >人
    <p v-for="(num, index2) in selectPeopleNums" :key="'p_' + index2">
      评选人{{ index2 + 1 }}：
      <Select
        transfer
        filterable
        filter-by-label
        class="w200"
        v-model="selectAllPeople[index2]"
        @on-change="handleChangeSelectPeople()"
      >
        <Option
          v-for="(people, pIndex) in checkOptionPeoples('', index2)"
          :key="'num_' + pIndex"
          :value="people.qq"
          >{{ people.nickname }}</Option
        >
      </Select>
      - [<a class="red" @click="doExport(null, index2)">下载</a>]
      {{ (answerObj = answerSelectedObj[index2 + ""]) && "" }}
      <template v-if="answerObj">
        - 数量：{{ answerObj.qqs.length }}
        <template v-if="answerObj.moveNum !== -1">
          - 复盘挪到第 {{ answerObj.moveNum + 1 }} 位小伙伴
        </template>
      </template>
    </p>
  </div>
</template>

<script>
import { cloneObj, saveTXT } from "js/utils.js";
export default {
  props: ["analyResult"],
  data() {
    return {
      selectNums: 3,
      selectAllPeople: {},
      selectPeoples: {},
      answerSelectedObj: {},
    };
  },
  computed: {
    numbers() {
      return this.$store.getters.getMembers();
    },
    selectPeopleNums() {
      let num = this.selectNums;
      let list = [];
      for (let i = 0; i < num; i++) {
        list.push(i);
      }
      return list;
    },
    answerNum() {
      return Math.ceil(this.numbers.length / this.selectNums);
    },
  },
  watch: {
    selectNums() {
      this.initData();
    },
  },
  methods: {
    initData() {
      for (let i = 0; i < this.selectNums; i++) {
        this.$set(this.selectAllPeople, i, "");
      }
    },
    handleChangeSelectPeople(index, index2) {
      let answerNum = this.answerNum;
      let name = "";
      let list = this.analyResult.filter((item) => item.count !== 0);
      list.sort((a, b) => (Math.random() > 0.5 ? 1 : -1));
      let list_len = list.length;
      let filterListObj = {};
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        filterListObj[numI] = {
          moveNum: -1,
          qqs: [],
        };
      }
      let listI = 0;
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        let selectQQ = name
          ? this.selectPeoples[name][numI]
          : this.selectAllPeople[numI];
        if (!selectQQ) {
          continue;
        }
        let filterList = [];
        for (let i = 0; i < answerNum && listI < list_len; i++) {
          let item = list[listI];
          listI += 1;
          if (item.qq === selectQQ) {
            let moveNum = numI + 1;
            if (moveNum === n) {
              moveNum = 0;
            }
            filterListObj[numI].moveNum = moveNum;
            filterListObj[moveNum].qqs.push(item.qq);
            continue;
          }
          filterList.push(item.qq);
        }
        filterListObj[numI].qqs.unshift(...filterList);
      }
      for (let numI = 0, n = this.selectPeopleNums.length; numI < n; numI++) {
        let name2 = numI + "";
        this.$set(this.answerSelectedObj, name2, filterListObj[numI]);
      }
    },
    doExport(index, index2) {
      let name = "";
      let answersObj = this.answersObj;
      let selectPeopleObj = this.selectAllPeople;
      let answerSelectedObj = this.answerSelectedObj;
      let isAnonymousQuestions = this.isAnonymousQuestions;
      let isHaveNO = this.isHaveNO;
      let fileName = "";
      let fileNameExt = ".txt";
      let numbers = this.analyResult.filter((item) => item.count !== 0);
      let qqObj = {};
      for (let i = 0, n = this.numbers.length; i < n; i++) {
        let item = this.numbers[i];
        qqObj[item.qq] = item;
      }

      if (index2 !== undefined) {
        let peopleQQ = selectPeopleObj[index2];
        if (!peopleQQ) {
          this.$Message.info("请先选择导出的人");
          return;
        }
        let qqItem = qqObj[peopleQQ];
        fileName = qqItem.nickname + "_" + fileName;
      }

      let name2 = "" + index2;
      let qqs = answerSelectedObj[name2].qqs;
      let list = this.analyResult;
      list = list.filter((item) => qqs.includes(item.qq));

      let str2 = "";
      for (let i = 0, n = list.length; i < n; i++) {
        let item = list[i];
        let nickname = item.nickname;
        let title = item.title || "";
        let content = item.content || "";
        str2 += `@${nickname}\n${title.trim()}\n${content.trim()}\n\n`;
      }
      saveTXT(str2, fileName + fileNameExt);
    },
    checkOptionPeoples(name, index2) {
      let extqqs = [];
      for (let i = 0; i < this.selectNums; i++) {
        if (i === index2) {
          continue;
        }
        let qq = name ? this.selectPeoples[name][i] : this.selectAllPeople[i];
        if (qq) {
          extqqs.push(qq);
        }
      }
      return this.numbers.filter((people) => !extqqs.includes(people.qq));
    },
  },
};
</script>

<style></style>
